package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Visitor;
import com.itheima.service.mongo.VisitorService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

//查看今日访客
@Service
public class VisitorServiceImpl implements VisitorService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //lastAccessTime 要求是时间的long表示法
    @Override
    public List<Visitor> findVisitorList(Long userId, String lastAccessTime) {
        //1. 构造查询条件
        Query query = new Query(
                Criteria.where("userId").is(userId).and("date").gt(Long.parseLong(lastAccessTime))
        ).skip(0).limit(4);

        //2. 查询访客列表
        List<Visitor> visitorList = mongoTemplate.find(query, Visitor.class);
        return visitorList;
    }
}